package com.vecharm.app.ui

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.provider.Settings
import android.util.Log
import com.tencent.sonic.sdk.SonicConfig
import com.tencent.sonic.sdk.SonicEngine
import com.vecharm.app.BuildConfig
import com.vecharm.app.R
import com.vecharm.app.bean.SessionBean
import com.vecharm.app.manager.SessionManager
import com.vecharm.app.ui.cloudshelf.CloudShelfActivity
import com.vecharm.app.ui.smartlable.SmartLabelActivity
import com.vecharm.app.ui.web.SonicRuntimeImpl
import com.vecharm.app.utils.SDCardHelper
import kotlinx.android.synthetic.main.activity_welcome.*

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 */

class WelcomeActivity : BaseShowActivity() {

    override fun getLayoutId() = R.layout.activity_welcome

    private val PERMISSION_REQUEST_CODE_STORAGE = 1


    override fun onInitView() {
        super.onInitView()

        serial.text = "序列号：" + Settings.Secure.getString(contentResolver, Settings.Secure.ANDROID_ID).toUpperCase()

        SessionManager.callBack = callBack

        checkInit()

//        Log.e("FileCache1", "${SDCardHelper.getSDCardSize()}->sd")
//        Log.e("FileCache1", "${SDCardHelper.getAppFileSize()}->appFile")

    }


    private fun checkInit() {

        if (hasPermission()) {
            init()
        } else {
            requestPermission()
        }
    }

    override fun onNetworkChange(isConnected: Boolean, netType: Int) {
        SessionManager.request()
    }

    private val callBack = { isSucceed: Boolean, desc: String? ->

        if (isSucceed) {
            checkInit()
        }
        status.text = "验证状态：$desc"


    }

    private fun jumpMain() {
        SessionManager.callBack = null
        when (BuildConfig.DeviceType) {
            0 -> startActivity(Intent(this, SmartLabelActivity::class.java))
            1 -> startActivity(Intent(this, CloudShelfActivity::class.java))
        }
        finish()
    }


    private fun init() {
        // init sonic engine
        if (!SonicEngine.isGetInstanceAllowed()) {
            SonicEngine.createInstance(SonicRuntimeImpl(application), SonicConfig.Builder().build())
        }

        //如果缓存有数据，直接跳到主界面
        if (SessionBean.getSession().isValid) {
            jumpMain()
        }
    }


    /* private fun hasPermission(): Boolean {
         return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
             checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
         } else true
     }*/
    private fun hasPermission() = true


    private fun requestPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), PERMISSION_REQUEST_CODE_STORAGE)
        }
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
        if (PERMISSION_REQUEST_CODE_STORAGE == requestCode) {
            if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
                requestPermission()
            } else {
                init()
            }
            return
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
    }


}
